package Hibernate.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * FinalScore entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see Hibernate.dao.FinalScore
 * @author MyEclipse Persistence Tools
 */
public class FinalScoreDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(FinalScoreDAO.class);
	// property constants
	public static final String EXAM_ID = "examId";
	public static final String FIRST_SCORE = "firstScore";
	public static final String FINAL_SCORE = "finalScore";

	public void save(FinalScore transientInstance) {
		log.debug("saving FinalScore instance");
		Transaction tr = getSession().beginTransaction(); 
		try {
			getSession().save(transientInstance);
			tr.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}
	
	public void delete(FinalScore persistentInstance) {
		log.debug("deleting FinalScore instance");
		Transaction tr = getSession().beginTransaction();
		try {
			getSession().delete(persistentInstance);
			tr.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public FinalScore findById(java.lang.String id) {
		log.debug("getting FinalScore instance with id: " + id);
		Transaction tr = getSession().beginTransaction();
		try {
			FinalScore instance = (FinalScore) getSession().get(
					"Hibernate.dao.FinalScore", id);
			tr.commit();
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByExample(FinalScore instance) {
		log.debug("finding FinalScore instance by example");
		Transaction tr = getSession().beginTransaction();
		try {
			List results = getSession()
					.createCriteria("Hibernate.dao.FinalScore")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			tr.commit();
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FinalScore instance with property: " + propertyName
				+ ", value: " + value);
		Transaction tr = getSession().beginTransaction();
		try {
			String queryString = "from FinalScore as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByExamId(Object examId) {
		return findByProperty(EXAM_ID, examId);
	}

	public List findByFirstScore(Object firstScore) {
		return findByProperty(FIRST_SCORE, firstScore);
	}

	public List findByFinalScore(Object finalScore) {
		return findByProperty(FINAL_SCORE, finalScore);
	}

	public List findAll() {
		log.debug("finding all FinalScore instances");
		Transaction tr = getSession().beginTransaction();
		try {
			String queryString = "from FinalScore";
			Query queryObject = getSession().createQuery(queryString);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public FinalScore merge(FinalScore detachedInstance) {
		log.debug("merging FinalScore instance");
		try {
			FinalScore result = (FinalScore) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FinalScore instance) {
		log.debug("attaching dirty FinalScore instance");
		Transaction tr = getSession().beginTransaction();
		try {
			getSession().saveOrUpdate(instance);
			tr.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public void attachClean(FinalScore instance) {
		log.debug("attaching clean FinalScore instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}